package com.xingongchang.baolebei.activity;


import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.external.androidquery.callback.AjaxStatus;
import com.insthub.BeeFramework.activity.BaseActivity;
import com.insthub.BeeFramework.model.BusinessResponse;
import com.insthub.BeeFramework.view.RoundImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.model.ProtocolConst;
import com.xingongchang.baolebei.model.UserInfoModel;
import com.xingongchang.baolebei.protocol.SESSION;
import com.xingongchang.baolebei.protocol.USER_INFO;

public class ProfileActivity extends BaseActivity implements BusinessResponse {
	String ds = "";
    private SharedPreferences prefer;
    private SharedPreferences.Editor edit;
	private ImageView backButton,head_camera;
	private TextView title;
	private LinearLayout top_right_button;
	private TextView top_right_text;
	RoundImageView userIcon;
	public USER_INFO userinfo;
	private TextView heand_name;
	EditText user_name;
	private UserInfoModel mUserInfoModel;
	LinearLayout Update_button;
	ImageLoader imageLoader = ImageLoader.getInstance();
	CheckBox boy, girl;
	int sex = 1;
	String pic = "";
	String name;
	TextView update;
	TextView youxiang;
	Boolean flag = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_profile);
		findViewById();
		setListener();
		init();
	}
	private void findViewById() {
		backButton = (ImageView) findViewById(R.id.top_view_back);
		title = (TextView) findViewById(R.id.top_view_text);
		top_right_button = (LinearLayout) findViewById(R.id.top_right_button);
		top_right_text = (TextView) findViewById(R.id.top_right_text);
		userIcon = (RoundImageView) findViewById(R.id.userIcon);
		user_name = (EditText) findViewById(R.id.user_name);
		boy = (CheckBox) findViewById(R.id.boy);
		girl = (CheckBox) findViewById(R.id.girl);
		heand_name=(TextView) findViewById(R.id.heand_name);
//		prefer=getSharedPreferences("tuipian", 0);
//		edit = prefer.edit();
//		update=(TextView) findViewById(R.id.update);
		youxiang=(TextView) findViewById(R.id.youxiang);
		Update_button=(LinearLayout) findViewById(R.id.Update_button);
//		Toast.makeText(getApplicationContext(), ""+userinfo.user_photo, 0).show();
		head_camera=(ImageView)findViewById(R.id.head_camera);
		
	}

	private void setListener() {
		boy.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				boy.setChecked(true);
				girl.setChecked(false);
				sex = 1;
			}
		});

		girl.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				boy.setChecked(false);
				girl.setChecked(true);
				sex = 0;
			}
		});

		backButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				hideSystemKeyBoard(ProfileActivity.this, user_name);
				finish();
			}
		});

		top_right_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				name= user_name.getText().toString().trim();
				mUserInfoModel.setTopicUserInfo(name, pic, sex);
			}
		});
           
		head_camera.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog();
			}
		});
		Update_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent=new Intent(ProfileActivity.this,Update_pwd.class);
				startActivity(intent);
				overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);
			}
		});

	}

	private void init() {

		backButton.setVisibility(View.VISIBLE);
		top_right_button.setVisibility(View.VISIBLE);
		title.setText("个人资料");
		top_right_text.setText("完成");
		boy.setChecked(true);
		girl.setChecked(false);
		mUserInfoModel = new UserInfoModel(this);
		mUserInfoModel.addResponseListener(this);
		SESSION session=SESSION.getInstance();
		mUserInfoModel.getTopicUserInfo(session.uid);
		user_name.setText(name);
	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		if (url.endsWith(ProtocolConst.USER_INFO)) {
			userinfo = mUserInfoModel.userinfo;
			imageLoader.displayImage(userinfo.user_photo, userIcon);
			user_name.setText(userinfo.nickname);
			heand_name.setText(userinfo.user_name);
			youxiang.setText(userinfo.email);
			sex = userinfo.sex;
			if (sex == 1) {
				boy.setChecked(true);
				girl.setChecked(false);

			} else if (sex == 0) {
				boy.setChecked(false);
				girl.setChecked(true);
			}
		}else if(url.endsWith(ProtocolConst.EDIT_USER_INFO)){
			SESSION session=SESSION.getInstance();
			mUserInfoModel.getTopicUserInfo(session.uid);
			if(mUserInfoModel.responseStatus.succeed==1){
				Intent intent = new Intent();
				intent.putExtra("login", true);
				setResult(Activity.RESULT_OK, intent);  
				Toast.makeText(ProfileActivity.this, "资料修改成功",
						Toast.LENGTH_SHORT).show();
				finish();
			}else{
				Toast.makeText(ProfileActivity.this,mUserInfoModel.responseStatus.error_desc,
						Toast.LENGTH_SHORT).show();
			}
		
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == PHOTO_REQUEST_GALLERY) {
			// 从相册返回的数据
			if (data != null) {
				// 得到图片的全路径
				Uri uri = data.getData();
				crop(uri);
			}

		} else if (requestCode == PHOTO_REQUEST_CAREMA) {
			// 从相机返回的数据
			if (hasSdcard()) {
				crop(Uri.fromFile(tempFile));
			} else {
				Toast.makeText(ProfileActivity.this, "未找到存储卡，无法存储照片！",
						Toast.LENGTH_SHORT).show();
			}

		} else if (requestCode == PHOTO_REQUEST_CUT) {
			// 从剪切图片返回的数据
			if (data != null) {
				Bitmap bitmap = data.getParcelableExtra("data");
				userIcon.setImageBitmap(bitmap);
				pic = imgToBase64("", bitmap, "");
			}
			try {
				// 将临时文件删除
				tempFile.delete();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}

}
